/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.JavaBean.Customer;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class CustomerDAO extends BasicDAO<Customer> {
	/**
	 * CustomerDAO无参构造 重载父类构造器
	 */
	public CustomerDAO() {
		super();
	}

	/**
	 * CustomerDAO有参构造 重载父类构造器
	 */
	public CustomerDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}

	/**
	 * 新增新客户，并插入到数据库中，参数是Customer对象
	 */
	public int insertNewCus(Customer cus) {
		int count = 0;
		String sql = "Insert into customer values(?,?,?,?,?,?)";
		try {
			count = super.updateDML(sql, cus.getCusId(), cus.getCusName(), cus.getCorpoRepresentative(),
					cus.getRegiAddress(), cus.getEmail(), 0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * @apiNote 根据用户id删除客户 当数据库内不存在该客户时 返回 0 此时代表已被删除或者根本不存在 若要判断是其中哪一种状态
	 *          请使用get方法并设置excludeDelete为false
	 */
	public int deleteCusById(int cusId) {
		int count = 0;
		String sql = "update customer set isDelete = 1 where cusid = ?";
		try {
			count = super.updateDML(sql, cusId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * getCustomerInfoById(String cusId) 通过用户Id获取该用户所有信息
	 */
	public Customer getCustomerInfoById(int cusId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from customer where cusid = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<Customer> c = getInfoFromDB(sql.toString(), cusId);
		if (c.size() == 0) {
			return null;
		} else {
			return c.get(0);
		}
	}
	
	/**
	 * getCustomerInfoByEmail(String email) 通过用户邮箱获取该用户所有信息
	 */
	public Customer getCustomerInfoByEmail(String email, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from customer where email = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<Customer> c = getInfoFromDB(sql.toString(), email);
		if (c.size() == 0) {
			return null;
		} else {
			return c.get(0);
		}
	}

	/**
	 * updateCusInfo(Customer cus) 根据客户对象所修改的数据来更新数据
	 */
	public int updateCusInfo(Customer cus) {
		int count = 0;
		boolean haveFirst = false;
		StringBuffer sql = new StringBuffer("update customer set ");
		Customer cusDB = getCustomerInfoById(cus.getCusId(), true);

		if (!cus.getCusName().equals(cusDB.getCusName())) {

			sql.append("cusname = '" + cus.getCusName() + "' ");
		}
		if (!cus.getCorpoRepresentative().equals(cusDB.getCorpoRepresentative())) {
			if(haveFirst) sql.append(",");
			haveFirst = true;
			sql.append("CorpoRepresentative = '" + cus.getCorpoRepresentative() + "' ");
		}
		if (!cus.getRegiAddress().equals(cusDB.getRegiAddress())) {
			if(haveFirst) sql.append(",");
			haveFirst = true;
			sql.append("RegiAddress = '" + cus.getRegiAddress() + "' ");
		}
		if (!(cus.getEmail() == cusDB.getEmail())) {
			if(haveFirst) sql.append(",");
			haveFirst = true;
			sql.append("email = '" + cus.getEmail() + "' ");
		}
		sql.append("where cusid = ? ");
		try {
			count = updateDML(sql.toString(), cus.getCusId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;

	}

	/**
	 * getCustomersBySQLFromDB(String sql,Object...obj)
	 * 通过自行传入sql语句以及可变参数获取该sql语句需要进行操作的变量 来进行查询
	 * 
	 * 返回一个Customer类的集合
	 * 
	 * @apiNote 注意：只能传入SELECT * 的语句，否则会报参数不匹配异常
	 */
	public ArrayList<Customer> getCustomersBySQLFromDB(String sql, Object... obj) {
		return getInfoFromDB(sql, obj);
	}
	
	

	@Override
	public ArrayList<Customer> resultSetToArrayList(ResultSet re) {
		ArrayList<Customer> c = new ArrayList<Customer>();

		try {
			while (re.next()) {
				c.add(new Customer(re.getInt("cusId"), re.getString("cusName"), re.getString("corpoRepresentative"),
						re.getString("regiAddress"), re.getString("email")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return c;
	}

}
